//
//  CameraScreen.m
//  Gekko
//
//  Created by jose on 02/06/13.
//  Copyright (c) 2013 Jose Luis Lopez. All rights reserved.
//

#import "CameraScreen.h"
#import <AVFoundation/AVFoundation.h>

@interface CameraScreen ()

@end

@implementation CameraScreen

@synthesize buttonCamera, buttonFlash, buttonGallery, imagePicker, pickedImage;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.isFirstTime = YES;
    
    
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.showsCameraControls = NO;
    imagePicker.delegate = self;
    
/*
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    // iOS is going to calculate a size which constrains the 4:3 aspect ratio
    // to the screen size. We're basically mimicking that here to determine
    // what size the system will likely display the image at on screen.
    // NOTE: screenSize.width may seem odd in this calculation - but, remember,
    // the devices only take 4:3 images when they are oriented *sideways*.
    float cameraAspectRatio = 4.0 / 3.0;
    float imageWidth = floorf(screenSize.width * cameraAspectRatio);
    float scale = ceilf((screenSize.height / imageWidth) * 10.0) / 10.0;
    imagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale + 0.1, scale + 0.1);
*/
    [self.view setClipsToBounds:NO];
    [self.view addSubview:imagePicker.view];


    [imagePicker.cameraOverlayView setHidden:YES];
    [self performSelector:@selector(createOverlay) withObject:nil afterDelay:2.2];
}

-(void)viewDidAppear:(BOOL)animated {
 
    NSLog(@"VIEW DID APPEAR");
    malloc(2);
    [imagePicker viewDidAppear:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    
    NSLog(@"VIEW WILL APPEAR");
    malloc(2);
    [imagePicker viewWillAppear:YES];
    [imagePicker.cameraOverlayView setHidden:YES];
    [self performSelector:@selector(createOverlay) withObject:nil afterDelay:2.2];
}

-(void)viewDidDisappear:(BOOL)animated {
    
    [imagePicker.cameraOverlayView setHidden:YES];
}

- (void)createOverlay {
    
    if (self.isFirstTime) {
        UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, [[UIScreen mainScreen] bounds].size.height)];
        [overlay setBackgroundColor:[UIColor clearColor]];
        [overlay setAlpha:1];
        
        buttonFlash = [UIButton buttonWithType:UIButtonTypeCustom];
        [buttonFlash setFrame:CGRectMake(17, 20, 68, 33)];
        [buttonFlash setImage:[UIImage imageNamed:@"button_flash_auto"] forState:UIControlStateNormal];
        [buttonFlash addTarget:self action:@selector(flashPressed:) forControlEvents:UIControlEventTouchUpInside];
        [buttonFlash setTag:0];
        [overlay addSubview:buttonFlash];
        
        NSInteger addition = 0;
        if ([[UIScreen mainScreen] bounds].size.height > 480)
            addition = 88;
        buttonCamera = [UIButton buttonWithType:UIButtonTypeCustom];
        [buttonCamera setImage:[UIImage imageNamed:@"button_camera"] forState:UIControlStateNormal];
        [buttonCamera setFrame:CGRectMake(124, addition + 350, 72, 72)];
        [buttonCamera addTarget:self action:@selector(cameraPressed:) forControlEvents:UIControlEventTouchUpInside];
        [overlay addSubview:buttonCamera];
        
        buttonGallery = [UIButton buttonWithType:UIButtonTypeCustom];
        [buttonGallery setFrame:CGRectMake(17, addition + 350, 68, 68)];
        [buttonGallery setImage:[UIImage imageNamed:@"button_gallery"] forState:UIControlStateNormal];
        [buttonGallery addTarget:self action:@selector(galleryPressed:) forControlEvents:UIControlEventTouchUpInside];
        [overlay addSubview:buttonGallery];
        
        imagePicker.cameraOverlayView = overlay;
        [imagePicker.view bringSubviewToFront:imagePicker.cameraOverlayView];
    } else
        [imagePicker.cameraOverlayView setHidden:NO];
    
}

- (IBAction)flashPressed:(UIButton *)sender {

    if (sender.tag == 0) {
        
        [buttonFlash setImage:[UIImage imageNamed:@"button_flash_yes"] forState:UIControlStateNormal];
        [buttonFlash setTag:1];
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
    } else if (sender.tag == 1) {

        [buttonFlash setImage:[UIImage imageNamed:@"button_flash_no"] forState:UIControlStateNormal];
        [buttonFlash setTag:2];
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    } else {

        [buttonFlash setImage:[UIImage imageNamed:@"button_flash_auto"] forState:UIControlStateNormal];
        [buttonFlash setTag:0];
        imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
    }
}

- (IBAction)galleryPressed:(UIButton *)sender {
    
    UIImagePickerController *galleryController = [[UIImagePickerController alloc] init];
    galleryController.delegate = self;
    
    [self presentViewController:galleryController animated:YES completion:^(void) {
        
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [UIView animateWithDuration:0.1 animations:^(void) {
            [galleryController.view setFrame:CGRectMake(0, 20, 320, 480)];
        }];
    }];
}

- (IBAction)cameraPressed:(UIButton *)sender {
    
    [imagePicker takePicture];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)inImage editingInfo:(NSDictionary *)editingInfo {

    NSLog(@"Finish picking");
    
    pickedImage = inImage;
    
    if (imagePicker != picker) {
        [picker dismissViewControllerAnimated:NO completion:^(void) {
            
            [self performSegueWithIdentifier:@"PushToRetake" sender:inImage];
        }];
    } else
        [self performSegueWithIdentifier:@"PushToRetake" sender:inImage];
    
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"PushToRetake"]) {
        
        ((RetakeScreen *)segue.destinationViewController).image = (UIImage *)sender;
    }
}

@end
